package com.aier.common.service.impl;

import javax.annotation.Resource;
import net.eicp.ghostboy.futil.web.WebEmail;
import net.eicp.ghostboy.futil.web.bean.WebEmailBean;

import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.SysEmail;
import com.aier.admin.entity.mapper.SysEmailRowMapper;
import com.aier.common.dao.ICommonDAO;
import com.aier.common.service.ISysEmailService;
import com.aier.common.service.IUserSendEmailService;



@Component("userSendEmailService")
public class SysUserEmailServiceImpl_J implements IUserSendEmailService{
    @Resource(name = "commonDao")
    private ICommonDAO commonDao;
    
    @Resource(name = "sysEmailService")
    private ISysEmailService sysEmailService;
    
    @Resource(name = "sendMailService")
    private WebEmail webEmail;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }
    
    /**
     * 找回密码
     * 0客户邮箱，1用户账号，2key
     */
    private static final short TYPE_GET_PASSWORD=1;


    /**
     * 发送用户类型的邮件
     */
    @Override
    public void updateSendHtmlMail(SysEmail se) {
        WebEmailBean we=new WebEmailBean();
        we.setContext(se.getValue());
        we.setToEmail(se.getToEmail());
        
        switch(se.getTypeChild()){
            case TYPE_GET_PASSWORD:
                try {
                    boolean send_flag = webEmail.sendMail(we);
                    if(send_flag){
                        sysEmailService.updateForHistoryEmail(se);
                    }else{
                        sysEmailService.updateMailSize(se, se.getSize()+1);
                    }
                } catch (Exception e) {
                    sysEmailService.updateMailSize(se, se.getSize()+1);
                    e.printStackTrace();
                }
                break;
        }
    }
        
    

    /**
     *      增加  找回密码邮件
     *  stype:TYPE_GET_PASSWORD
     *  args:0客户邮箱，1用户账号，2key
     */
    @Override
    public int updateSendMail(short stype, Object... args) {
        switch(stype){
            case TYPE_GET_PASSWORD:
                //insert
                SysEmail se = new SysEmail();
                se.setToEmail((String) args[0]);
                se.setValue((String)args[1] + ":" + (String)args[2]);
                se.setType(TYPE_USER);
                se.setTypeChild(TYPE_GET_PASSWORD);
                se.setSize(0);
                commonDao.insert(
                            SysEmailRowMapper.getPreparedStatementCreator(se),
                            new GeneratedKeyHolder());
    
                break;
        }
        return 0;
    }



    public ISysEmailService getSysEmailService() {
        return sysEmailService;
    }



    public void setSysEmailService(ISysEmailService sysEmailService) {
        this.sysEmailService = sysEmailService;
    }



    public WebEmail getWebEmail() {
        return webEmail;
    }



    public void setWebEmail(WebEmail webEmail) {
        this.webEmail = webEmail;
    }

    
}
